package com.lb.shopguide.adapter;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.market.ActivityApplyBean;
import com.lb.shopguide.util.pic.ImageLoaderUtils;

import java.util.List;

public class AdapterActivityApply extends BaseQuickAdapter<ActivityApplyBean, BaseViewHolder> {
    private boolean isHistory;

    public AdapterActivityApply(int layoutResId, @Nullable List<ActivityApplyBean> data, boolean isHistory) {
        super(layoutResId, data);
        this.isHistory = isHistory;
    }

    @Override
    protected void convert(BaseViewHolder helper, final ActivityApplyBean item) {
        View view = helper.itemView;
        ImageView ivApplyPic = view.findViewById(R.id.iv_apply_pic);
        TextView tvApplyShop = view.findViewById(R.id.tv_apply_shop);
        TextView tvApplyAct = view.findViewById(R.id.tv_apply_act);
        TextView tvApplyTime = view.findViewById(R.id.tv_apply_time);
        TextView tvActTime = view.findViewById(R.id.tv_act_time);
        TextView tvCheck = view.findViewById(R.id.tv_check);
        RelativeLayout layoutCheck = view.findViewById(R.id.layout_check);
        ImageView ivIconCheck = view.findViewById(R.id.iv_check_icon);

        if (isHistory) {
            tvApplyTime.setVisibility(View.VISIBLE);
        } else {
            tvApplyTime.setVisibility(View.GONE);
        }
        tvApplyTime.setText("申请时间：" + item.getCreateTime());

        ImageLoaderUtils.display(mContext, ivApplyPic, item.getActivityDescPic());
        tvApplyShop.setText(item.getStoreName());
        tvApplyAct.setText("申请活动：" + item.getActivityName());
        tvActTime.setText("活动时间：" + item.getActBeginTime() + "至" + item.getActEndTime());
        Drawable drawableApplying = mContext.getResources().getDrawable(R.drawable.bg_round_corner_activity_run);
        Drawable drawableAgreed = mContext.getResources().getDrawable(R.drawable.bg_round_corner_green_radius_4);
        Drawable drawableOut = mContext.getResources().getDrawable(R.drawable.bg_round_corner_gray_4px);
        switch (item.getApplyStatus()) {
            case AppConstant.APPLY_ING:
                ivIconCheck.setVisibility(View.VISIBLE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layoutCheck.setBackground(drawableApplying);
                } else {
                    layoutCheck.setBackgroundDrawable(drawableApplying);
                }
                layoutCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnCheckClickListener != null) {
                            mOnCheckClickListener.onChecked(item.getApplyCode());
                        }
                    }
                });
                tvCheck.setText("去审核");
                break;
            case AppConstant.APPLY_AGREED:
                ivIconCheck.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layoutCheck.setBackground(drawableAgreed);
                } else {
                    layoutCheck.setBackgroundDrawable(drawableAgreed);
                }
                tvCheck.setText("已同意");
                break;
            case AppConstant.APPLY_REFUSED:
                ivIconCheck.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layoutCheck.setBackground(drawableApplying);
                } else {
                    layoutCheck.setBackgroundDrawable(drawableApplying);
                }
                tvCheck.setText("已拒绝");
                break;
            case AppConstant.APPLY_OUT_OF_DATED:
                ivIconCheck.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layoutCheck.setBackground(drawableOut);
                } else {
                    layoutCheck.setBackgroundDrawable(drawableOut);
                }
                tvCheck.setText("已过期");
                break;
        }
    }

    public interface OnCheckClickListener {
        void onChecked(String applyCode);
    }

    private OnCheckClickListener mOnCheckClickListener;

    public void setOnCheckClickListener(OnCheckClickListener listener) {
        mOnCheckClickListener = listener;
    }
}
